# Adogs
# 时间: 2021/7/25 13:51
import pygame
from actor.xiao_tie import XiaoTie
from game import SCREEN_WIDTH, SCREEN_HEIGHT, GAME_TITLE, GAME_ICON
from scene import ScenePassStatus
from scene.fail_scene import FailScene
from scene.win_scene import WinScene
from scene.tunnel_war_scene import TunnelWarScene
from scene.tunnel_war_scene_2 import TunnelWarScene2
from scene.welcome_scene import WelcomeScene


class TunnelWarfare:
    """
    游戏运行函数类
    方法:
    1.__init__()
    2.run()
    """
    def __init__(self):
        """
        游戏初始化
        """
        pygame.init()   # pygame初始化
        pygame.mixer.init()     # 游戏音乐功能的初始化
        self.clock = pygame.time.Clock()    # 计时器

        # 游戏窗口初始化
        self.surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE, 64)

        pygame.display.set_caption(GAME_TITLE)  # 游戏标题设置
        self.xiao_tie = XiaoTie(0, 0)   # 主角初始化

        # 游戏图标设置
        icon = pygame.image.load(GAME_ICON)
        pygame.display.set_icon(icon)

        # 游戏开始场景设置
        self.current_scene = WelcomeScene(self.xiao_tie)

    def run(self):
        """
        游戏运行函数
        :return: 无返回值
        """
        while True:
            down_flag = False

            # 检测事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        exit()
                    else:
                        down_flag = True

            key_list = pygame.key.get_pressed()     # 获取按键情况
            current_screen = self.current_scene.get_current_surface()   # 获取场景视窗图
            self.surface.blit(current_screen, (0, 0))   # 绘制窗口
            self.current_scene.run(down_flag, key_list)     # 场景运行

            # 切换场景
            pass_status, next_scene_name = self.current_scene.get_pass_status()

            if pass_status != ScenePassStatus.normal:
                if next_scene_name:
                    next_scene = globals()[next_scene_name](self.xiao_tie)
                    self.current_scene = next_scene
                else:
                    break

            self.clock.tick(20)     # 游戏绘制的最大帧率
            pygame.display.flip()
